package com.example.taximeter.controller;

import com.example.taximeter.bean.*;
import com.example.taximeter.service.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 参数文件
 */
@RestController
@RequestMapping(value = "/paramfile", method = RequestMethod.POST)
public class ParamFileController {

    private ParamFileService paramFileService;
    private FactService factService;
    private TaximeterTypeService taximeterTypeService;
    private PriceService priceService;
    private UserService userService;
//    private final static Logger logger = LoggerFactory.getLogger(ParamFileController.class);



    @Autowired
    public ParamFileController (ParamFileService paramFileService, FactService factService,TaximeterTypeService taximeterTypeService,PriceService priceService,UserService userService){
        this.paramFileService = paramFileService;
        this.factService = factService;
        this.taximeterTypeService = taximeterTypeService;
        this.priceService = priceService;
        this.userService =userService;
    }

    //查看参数文件列表
    @RequestMapping(value = "/all")
    public ReturnInfo findParamFile(@RequestParam(value = "pageNum", defaultValue = "0") String pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "0") String pageSize,
                                       @RequestBody ParamFileView paramFileView) {
        //添加日志
        userService.addUserLog(7,paramFileView.toString());
        return paramFileService.findParamFileALL(Integer.valueOf(pageNum), Integer.valueOf(pageSize), paramFileView);
    }

    //查看参数文件列表(待审核)
    @RequestMapping(value = "/notcheck")
    public ReturnInfo findParamFileNotCheck(@RequestParam(value = "pageNum", defaultValue = "0") String pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "0") String pageSize) {
        return paramFileService.findParamFileALLNotCheck(Integer.valueOf(pageNum), Integer.valueOf(pageSize));
    }

    //查看参数文件列表（已审核）
    @RequestMapping(value = "/checked")
    public ReturnInfo findParamFileChecked(@RequestParam(value = "pageNum", defaultValue = "0") String pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "0") String pageSize) {
        return paramFileService.findParamFileALLChecked(Integer.valueOf(pageNum), Integer.valueOf(pageSize));
    }

    //查看参数文件日志列表
    @RequestMapping(value = "/log")
    public ReturnInfo findParamFileLogAll(@RequestParam(value = "pageNum", defaultValue = "0") String pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "0") String pageSize,
                                          @RequestBody ParamFileLog paramFileLog) {
        //添加日志
        userService.addUserLog(7,"参数文件日志"+paramFileLog.toString());
        return paramFileService.findParamFileLogALL(Integer.valueOf(pageNum), Integer.valueOf(pageSize), paramFileLog);
    }

    //交通部审核
    @RequestMapping(value = "/trafficsh")
    public ReturnInfo trafficCheck(@RequestBody ParamFile paramFile){
        if(paramFileService.trafficCheck(paramFile).equals("成功")) {
            //添加日志
            userService.addUserLog(3,paramFile.toString());
            ParamFileLog paramFileLog = new ParamFileLog();
            paramFileLog.setParamid(paramFile.getId());
            paramFileLog.setOpertype(3);
            paramFileLog.setOpercontent("交通部审核通过");
            if(paramFileService.addParamFileLog(paramFileLog).equals("失败")){
               return ReturnInfo.info1("添加成功但生成日志失败");
            }else {

                return ReturnInfo.info1("成功");
            }
        }else {

            return ReturnInfo.info1("失败");
        }
    }

    //财务部审核
    @RequestMapping(value = "/pricesh")
    public ReturnInfo priceCheck(@RequestBody ParamFile paramFile){
        if(paramFileService.priceCheck(paramFile).equals("成功")) {
            //添加日志
            userService.addUserLog(3,paramFile.toString());
            ParamFileLog paramFileLog = new ParamFileLog();
            paramFileLog.setParamid(paramFile.getId());
            paramFileLog.setOpertype(3);
            paramFileLog.setOpercontent("财务部审核通过");
            if(paramFileService.addParamFileLog(paramFileLog).equals("失败")){
                return ReturnInfo.info1("添加成功但生成日志失败");
            }else {
                return ReturnInfo.info1("成功");
            }
        }else {

            return ReturnInfo.info1("失败");
        }

    }

    //计量部审核
    @RequestMapping(value = "/metrologysh")
    public ReturnInfo metrologyCheck(@RequestBody ParamFile paramFile){
        if(paramFileService.metrologyCheck(paramFile).equals("成功")) {
            //添加日志
            userService.addUserLog(3,paramFile.toString());
            ParamFileLog paramFileLog = new ParamFileLog();
            paramFileLog.setParamid(paramFile.getId());
            paramFileLog.setOpertype(3);
            paramFileLog.setOpercontent("计量部审核通过");
            if(paramFileService.addParamFileLog(paramFileLog).equals("失败")){
                return ReturnInfo.info1("添加成功但生成日志失败");
            }else {
                return ReturnInfo.info1("成功");
            }
        }else {

            return ReturnInfo.info1("失败");
        }

    }

    //添加物价文件信息
    @RequestMapping(value = "/add")
    public ReturnInfo addTaximeter(@RequestBody ParamFile paramFile){
        String mes = paramFileService.addParamFile(paramFile);
        if(mes.equals("成功")){
            //添加日志
            userService.addUserLog(4,paramFile.toString());
//            logger.info("新建物价文件"+paramFile.toString());
            ParamFileLog paramFileLog = new ParamFileLog();
            paramFileLog.setParamid(paramFile.getId());
            paramFileLog.setOpertype(1);
            paramFileLog.setOpercontent("计价器厂商"+factService.findFactnameByFactid(paramFile.getFactid())+"\n价格类型"+priceService.findPricetypeByPriceid(paramFile.getPriceid())
            +"\n计价器型号"+taximeterTypeService.findJjqtypeByJjqtypeid(paramFile.getJjqtypeid())+"\n版本号"+paramFile.getVersion()+"\n物价文件编号"+paramFile.getPricefileno()+"\n文件名称"+paramFile.getFilename());
            if(paramFileService.addParamFileLog(paramFileLog).equals("失败")){
                mes = "添加成功但生成日志失败";
            }else {
                mes = "成功";
            }

        }
        return ReturnInfo.info1(mes);

    }

    //修改物价文件信息
    @RequestMapping(value = "/update")
    public ReturnInfo updateTaximeter(@RequestBody ParamFileView paramFileView){
        String mes = paramFileService.updateParamFile(paramFileView);
        if(mes.equals("成功")){
            //添加日志
            userService.addUserLog(5,paramFileView.toString());
//            logger.info("修改物价文件"+paramFileView.toString());
            ParamFileLog paramFileLog = new ParamFileLog();
            paramFileLog.setParamid(paramFileView.getId());
            paramFileLog.setOpertype(2);
            paramFileLog.setOpercontent("计价器厂商为"+factService.findFactnameByFactid(paramFileView.getFactid())+"\n价格类型为"+priceService.findPricetypeByPriceid(paramFileView.getPriceid())
                    +"\n计价器型号为"+taximeterTypeService.findJjqtypeByJjqtypeid(paramFileView.getJjqtypeid())+"\n版本号为"+paramFileView.getVersion()+"\n物价文件编号为"+paramFileView.getPricefileno()+"\n文件名称为"+paramFileView.getFilename());
            if(paramFileService.addParamFileLog(paramFileLog).equals("失败")){
                mes = "添加成功但生成日志失败";
            }else {
                mes = "成功";
            }

        }
        return ReturnInfo.info1(mes);


    }

    //删除物价文件信息
    @RequestMapping(value = "/delete/{id}")
    public ReturnInfo deleteParamfile(@PathVariable String id){
        String mes = paramFileService.deleteParamfile(id);
        if(mes.equals("成功")){
            //添加日志
            userService.addUserLog(6,"物价文件信息"+String.valueOf(id));
//            logger.info("修改物价文件"+id);
        }
        return ReturnInfo.info1(mes);
    }

    //查询版本号列表
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<ParamFileView> findParamfileList(){
        return paramFileService.findParamfileList();
    }

}
